package com.android.canvasproject.view.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.android.canvasproject.R;
import com.android.canvasproject.model.listeners.PostitListener;

public class ProjectModelCanvasUtil {

	private static final float PROPORTION_JUSTIFICATIONS = 0.283f;
	private static final float PROPORTION_OBJESMART = 0.23f; // 22.3%
	private static final float PROPORTION_PRODUCT = 0.283f;
	private static final float PROPORTION_STAKEHOLDERS = 0.365f;
	private static final float PROPORTION_ASSUMPTIONS = 0.365f;
	private static final float PROPORTION_RESTRICTIONS = 0.2f; // 18.3%
	private static final float PROPORTION_RISKS = 0.365f;
	private static final float PROPORTION_TIMELINE = 0.405f;

	@SuppressWarnings("unused")
	private static final float PROPORTION_BENEFITS = 0.494f;
	@SuppressWarnings("unused")
	private static final float PROPORTION_REQUIREMENTS = 0.717f;
	@SuppressWarnings("unused")
	private static final float PROPORTION_TEAM = 0.452f;
	@SuppressWarnings("unused")
	private static final float PROPORTION_DELIVERYGROUP = 0.452f;
	@SuppressWarnings("unused")
	private static final float PROPORTION_COSTS = 0.23f;

	private ProjectModelCanvasUtil() { }

	public static void drawQuadrants(final Activity activity){
		RelativeLayout canvas = (RelativeLayout) activity.findViewById(R.id.project_model_canvas);
		canvas.setOnClickListener(new PostitListener(activity));
		canvas.setOnLongClickListener(new PostitListener(activity));
		canvas.setOnTouchListener(new PostitListener(activity));

		int deviceWidthPx = activity.getResources().getDisplayMetrics().widthPixels;
		int deviceHeightPx = activity.getResources().getDisplayMetrics().heightPixels;

		float deviceWidthDp = convertPixelsToDp(deviceWidthPx, activity);
		float deviceHeightDp = convertPixelsToDp(deviceHeightPx, activity);
		
		System.out.println("################### W - DP: "+deviceWidthDp);
		System.out.println("################### H - DP: "+deviceHeightDp);

		System.out.println("################### W - PX: "+deviceWidthPx);
		System.out.println("################### H - PX: "+deviceHeightPx);

		int colWidth = 0;

		if(deviceWidthPx > deviceWidthDp){
			colWidth = (int) (deviceWidthPx / 5);
		} else {
			colWidth = (int) (deviceWidthDp / 5);
		}

		//DEFININDO LARGURA DE CADA COLUNA
		LinearLayout col01 = (LinearLayout) activity.findViewById(R.id.pmc_column_01);
		col01.setLayoutParams(new LayoutParams(colWidth - 2, LayoutParams.MATCH_PARENT));
		
		LinearLayout col02 = (LinearLayout) activity.findViewById(R.id.pmc_column_02);
		col02.setLayoutParams(new LayoutParams(colWidth, LayoutParams.MATCH_PARENT));
		
		LinearLayout col03 = (LinearLayout) activity.findViewById(R.id.pmc_column_03);
		col03.setLayoutParams(new LayoutParams(colWidth * 2, LayoutParams.MATCH_PARENT));
		
		LinearLayout col04 = (LinearLayout) activity.findViewById(R.id.pmc_column_04);
		col04.setLayoutParams(new LayoutParams(colWidth, LayoutParams.MATCH_PARENT));
		
		LinearLayout col05 = (LinearLayout) activity.findViewById(R.id.pmc_column_05);
		col05.setLayoutParams(new LayoutParams(colWidth, LayoutParams.MATCH_PARENT));
		
		LinearLayout col06 = (LinearLayout) activity.findViewById(R.id.pmc_column_06);
		col06.setLayoutParams(new LayoutParams(colWidth, LayoutParams.MATCH_PARENT));

		int colHeight = 0;

		if(deviceHeightPx > deviceHeightDp){
			colHeight = deviceHeightPx;
		} else {
			colHeight = (int) deviceHeightDp;
		}

		//DEFININDO ALTURA QUADRANTES COLUNA 01
		View justifications = activity.findViewById(R.id.pmc_justifications);
		justifications.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_JUSTIFICATIONS)));

		View objSmart = activity.findViewById(R.id.pmc_objsmart);
		objSmart.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_OBJESMART))); //22.3%

		View benefits = activity.findViewById(R.id.pmc_benefits);
		benefits.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //49.4%

		//DEFININDO ALTURA QUADRANTES COLUNA 02
		View product = activity.findViewById(R.id.pmc_product);
		product.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_PRODUCT)));

		View requirements = activity.findViewById(R.id.pmc_requirements);
		requirements.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //71.7%
		
		//DEFININDO ALTURA QUADRANTES COLUNA 04
		View stakeholders = activity.findViewById(R.id.pmc_stakeholders);
		stakeholders.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_STAKEHOLDERS)));

		View team = activity.findViewById(R.id.pmc_team);
		team.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //45.2%
		//DEFININDO ALTURA QUADRANTES COLUNA 05
		View assumptions = activity.findViewById(R.id.pmc_assumptions);
		assumptions.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_ASSUMPTIONS)));

		View deliveryGroup = activity.findViewById(R.id.pmc_deliverygroup);
		deliveryGroup.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //45.2%
		
		//DEFININDO ALTURA QUADRANTES COLUNA 03
		View restrictions = activity.findViewById(R.id.pmc_restrictions);
		restrictions.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_RESTRICTIONS))); //18.3%
		
		//DEFININDO ALTURA QUADRANTES COLUNA 06
		View risks = activity.findViewById(R.id.pmc_risks);
		risks.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_RISKS)));

		View timeLine = activity.findViewById(R.id.pmc_timeline);
		timeLine.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (colHeight * PROPORTION_TIMELINE)));

		View costs = activity.findViewById(R.id.pmc_costs);
		costs.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //23%
	}

	public static float convertPixelsToDp(float px, Context ctx) {
		return px / (ctx.getResources().getDisplayMetrics().densityDpi / 160f);
	}

	public static float convertDpToPixels(float dp, Context ctx) {
		return dp * (ctx.getResources().getDisplayMetrics().densityDpi / 160f);
	}
}